package app.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;

import de.vdheide.mp3.FrameDamagedException;
import de.vdheide.mp3.ID3v2DecompressionException;
import de.vdheide.mp3.ID3v2IllegalVersionException;
import de.vdheide.mp3.ID3v2WrongCRCException;
import de.vdheide.mp3.MP3File;
import de.vdheide.mp3.MP3Properties;
import de.vdheide.mp3.NoMP3FrameException;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.drawable.shapes.PathShape;
import android.media.MediaMetadataRetriever;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import app.musicPlayer.domain.Ajustes;
import app.musicPlayer.domain.CancionesSistema;
import app.musicPlayer.domain.SocialMusicPlayerActivity;
import app.persistencia.CancionLista;

public class Utiles {

	public static final int SELECT_FROM_ALBUM  = 1;
	public static final int SELECT_FROM_ARTIST = 2;
	public static final int NON_SELECT         = 3;
	public static final String FACTORDESELECCION = "FactorSeleccion";
	public static final String VALOR             = "valor";
	
	public static void showEnProximasActualizaciones(Activity context) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();
		alertDialog.setTitle("En Desarrollo");
		alertDialog.setMessage("En proximas actualizaciones esta funcionalidad sera añadida");
	    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
	    	public void onClick(DialogInterface dialog, int which) {
			} 
	    });
	 	alertDialog.show();
	}
	
	public static boolean getFacebook(Context context){
		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
		return p.getBoolean("facebook", false);
	}
	public static void setFacebook(Context context,boolean b){
		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
		p.edit().putBoolean("facebook", b).commit();		
	}
	public static boolean getTwitter(Context context){
		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
		return p.getBoolean("twitter", false);
	}
	
	public static boolean getRandom(Context context){
		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
		return p.getBoolean("random", false);
	}
	
	public static boolean getRepetirTodas(Context context){
		SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
		return p.getBoolean("repetirTodas", false);
	}
	
	public static void showMessage(Activity context, String message) {
        Toast toast =
                Toast.makeText(context,
                        message, Toast.LENGTH_SHORT);
        toast.show();
	}
	public static ArrayList<String> giveMeAllMp3FilesOverSdCard() {
		return giveMeAllMp3OverDirectory("/mnt/sdcard/");
	}
	public static ArrayList<String> giveMeAllMp3OverDirectory(String path) {
		ArrayList<String> paths = new ArrayList<String>();
		ArrayList<File> directory = new ArrayList<File>();
		File root = new File(path);
		directory = getDirs(root.listFiles());
		for(int i = 0; i < directory.size(); i++) {
			paths.addAll(giveMeAllMp3OverDirectory(directory.get(i).getPath()));
		}
		paths.addAll(getMp3(root.listFiles()));
		return paths;
	}

	private static ArrayList<String> getMp3(File[] files) {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; files != null &&i < files.length; i++) {
			if(files[i].getPath().toString().endsWith(".mp3")) {
		        String path = files[i].getPath().substring(5);
				list.add(path);
			}
		}
		return list;
	}
	private static ArrayList<File>getDirs(File[] files) {
		ArrayList<File> dirs = new ArrayList<File>();
		for(int i = 0; files!=null&& i < files.length; i++) {
			if(files[i].isDirectory())
				dirs.add(files[i]);
		}
		return dirs;
	}
	/*
	 * MediaScan
	 */
	public static String getTittle(String path) {
		MediaMetadataRetriever mmr = new MediaMetadataRetriever();
		mmr.setDataSource(path);
		String tittle = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
		if(tittle == null)
			return (new File(path).getName());
		else return tittle;
	}
	public static String getAlbum(String path) {
		MediaMetadataRetriever mmr = new MediaMetadataRetriever();
		mmr.setDataSource(path);
		String album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
		if(album ==null || album.equals(""))
			return ("Deconocido");
		else return album;
	}
	public static int getDuration(String path) {
		MediaMetadataRetriever mmr = new MediaMetadataRetriever();
		mmr.setDataSource(path);
		String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
		if(duration == null)
			return (-1);
		else return Integer.parseInt(duration);
	}
	public static String getArtist(String path) {
		MediaMetadataRetriever mmr = new MediaMetadataRetriever();
		mmr.setDataSource(path);
		String artist= mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
		if(artist == null || artist.equals(""))
			return ("Deconocido");
		else return artist;
	}
	public static ArrayList<CancionLista> giveMp3Files() {
		ArrayList<String>path = giveMeAllMp3FilesOverSdCard();
		ArrayList<CancionLista> mp3s = new ArrayList<CancionLista>();
		for(int i = 0; i < path.size(); i++){
			mp3s.add(new CancionLista(path.get(i), i));
		}
		return mp3s;
	}
	/*
	 * 		final CancionesSistema s = CancionesSistema.getIntances();
        final ProgressDialog dialog = ProgressDialog.show(SocialMusicPlayerActivity.this, "", 
                "Cargando canciones, espere por favor...", true);
    	Thread t = new Thread() {
    		public void run() {
    			s.setSongs(Utiles.giveMp3Files());
    	    	dialog.dismiss();
    		}
    	};
        t.start();
	 */
	public static String reduce(String p,int b)
	{
		if(p.length() < b)
			return p;
		else 
			return (p.substring(0, b-3)+"...");
	} 
}
